package org.hackystat.devcathlon.ui;

import java.util.Timer;
import java.util.logging.Logger;
import org.apache.wicket.Page;
import org.apache.wicket.Request;
import org.apache.wicket.Response;
import org.apache.wicket.Session;
import org.apache.wicket.protocol.http.WebApplication;
import org.hackystat.devcathlon.configuration.ConfigurationException;
import org.hackystat.devcathlon.configuration.ConfigurationManager;
import org.hackystat.devcathlon.engine.event.EventManager;
//import org.hackystat.devcathlon.engine.initialization.Initialization;
import org.hackystat.devcathlon.engine.match.Match;
import org.hackystat.devcathlon.engine.match.MatchManager;
import org.hackystat.devcathlon.ui.page.DevcathlonPageAuthentication;
import org.hackystat.devcathlon.ui.page.login.LoginPage;
import org.hackystat.utilities.logger.HackystatLogger;

/**
 * The top-level web application instance for this Devcathlon.
 * 
 * @author Philip Johnson
 */
public class DevcathlonApplication extends WebApplication {

  /** Holds the HackystatLogger for this Service. */
  private Logger logger;
  /** The DPD host. */
  private String dpdHost;
  /** The sensorbase host. */
  private String sensorbaseHost;

  /**
   * Creates a DevcathlonApplication, initializing the Devcathlon configuration if necessary.
   * 
   * @throws ConfigurationException If the configuration is bad.
   */
  public DevcathlonApplication() throws ConfigurationException {
    this.logger = HackystatLogger.getLogger("org.hackystat.devcathlon", "devcathlon");
    ConfigurationManager configManager = ConfigurationManager.getInstance();
    this.dpdHost = configManager.getConfiguration().getHackystat().getDailyprojectdata();
    this.sensorbaseHost = configManager.getConfiguration().getHackystat().getSensorbase();
    
    
    // Undo comment for insert of test data.
//    try {
//      new Initialization();
//      this.logger.info("Successfully intialized users in configuration.xml");
//    }
//    catch (Exception e) {
//      e.printStackTrace();
//      this.logger.info(e.getMessage());
//      this.logger.info("Initializing content failed");
//    }
    

    // creates scheduled tasks for events
    for (Match match : MatchManager.getInstance().getAllMatches()) {
      EventManager.getInstance().createEventTasks(match, match.getEvents());
    }
    Timer timer = new Timer();
    timer.schedule(new CancelTask(), 0, 60000);
  }
  
  /**
   * Creates a DevcathlonApplication, initializing the Devcathlon configuration if necessary.
   * 
   * @param dailyProjectDataHost The daily project data host.
   * @param sensorBaseHost The sensor base hosts.
   * @throws ConfigurationException If the configuration is bad. 
   */
  public DevcathlonApplication(String dailyProjectDataHost, String sensorBaseHost) 
  throws ConfigurationException {
    this.logger = HackystatLogger.getLogger("org.hackystat.devcathlon", "devcathlon");
    this.dpdHost = dailyProjectDataHost;
    this.sensorbaseHost = sensorBaseHost;
  }

  /**
   * Returns the home page for this application (SigninPage).
   * 
   * @return The home page.
   */
  @Override
  public Class<? extends Page> getHomePage() {
    return LoginPage.class;
  }

  /**
   * Defines DevcathlonSession as the session instance created in this app.
   * 
   * @param request The request.
   * @param response The response.
   * @return The current DevcathlonSession instance.
   */
  @Override
  public Session newSession(Request request, Response response) {
    return new DevcathlonSession(request);
  }

  /**
   * Enforces authentication for all non-Login pages.
   */
  @Override
  public void init() {
    getSecuritySettings().setAuthorizationStrategy(new DevcathlonPageAuthentication());
    // Disable Wicket Ajax Debugger.
    getDebugSettings().setAjaxDebugModeEnabled(false);
    super.init();
  }

  /**
   * Returns the logger for this service.
   * 
   * @return The logger.
   */
  public Logger getLogger() {
    return this.logger;
  }

  /**
   * Returns the DPD host.
   * 
   * @return The DPD host.
   */
  public String getDailyProjectDataHost() {
    return this.dpdHost;
  }

  /**
   * Returns the sensorbase host.
   * 
   * @return The sensorbase host.
   */
  public String getSensorBaseHost() {
    return this.sensorbaseHost;
  }
}
